package com.example.service.impl;
import com.example.mapper.ActivityMapper;
import com.example.mapper.HeadMapper;
import com.example.mapper.MuseumMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.dto.HeadDto;
import com.example.pojo.entity.Activity;
import com.example.pojo.entity.Head;
import com.example.pojo.entity.Museum;
import com.example.pojo.entity.User;
import com.example.service.HeadService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HeadServiceImpl implements HeadService {

    @Resource
    HeadMapper headMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    MuseumMapper museumMapper;
    @Resource
    ActivityMapper activityMapper;

    @Override
    public void insert(HeadDto headDto) {
        Head head = new Head();
        head.setUserId(userMapper.selectByName1(headDto.getName()).getId());
        head.setTitle(headDto.getTitle());
        head.setMuseumId(museumMapper.selectByName(headDto.getMuseum()).getId());
        head.setActivityId(activityMapper.selectByName(headDto.getActivity()).getId());

        headMapper.insert(head);
    }

    @Override
    public void updateById(Head head) {

        headMapper.updateById(head);
    }

    @Override
    public void removeById(Integer id) {

        headMapper.removeById(id);
    }

    @Override
    public void removeBatchByIds(List<Integer> ids) {
        for (Integer id : ids){
            headMapper.removeById(id);
        }
    }

    @Override
    public List<Head> list() {
        List<Head> list = headMapper.list();
        return list;
    }

    @Override
    public HeadDto getById(Integer id) {
        Head head = headMapper.getById(id);
        HeadDto headDto = new HeadDto();
        headDto.setId(head.getId());
        headDto.setName(userMapper.selectById(id).getName());
        headDto.setTitle(head.getTitle());
        headDto.setMuseum(museumMapper.getById(id).getName());
        headDto.setActivity(activityMapper.getById(id).getName());
        headDto.setEmail(userMapper.selectById(id).getEmail());
        headDto.setPhone(userMapper.selectById(id).getPhone());
        return headDto;
    }

    @Override
    public PageInfo<HeadDto> selectByPage(Integer pageNum, Integer pageSize, String name, String title, String museum, String activity) {
        PageHelper.startPage(pageNum, pageSize);

        Integer userId = null;
        Integer museumId = null;
        Integer activityId = null;

        if (name != null && !name.isEmpty()) {
            User user = userMapper.selectByName1(name);
            if (user != null) {
                userId = user.getId();
            } else {
                // 如果用户查询为空，直接返回空的 PageInfo
                return new PageInfo<>(new ArrayList<>());
            }
        }

        if (museum != null && !museum.isEmpty()) {
            Museum museumEntity = museumMapper.selectByName(museum);
            if (museumEntity != null) {
                museumId = museumEntity.getId();
            } else {
                // 如果博物馆查询为空，直接返回空的 PageInfo
                return new PageInfo<>(new ArrayList<>());
            }
        }

        if (activity != null && !activity.isEmpty()) {
            Activity activityEntity = activityMapper.selectByName(activity);
            if (activityEntity != null) {
                activityId = activityEntity.getId();
            } else {
                // 如果活动查询为空，直接返回空的 PageInfo
                return new PageInfo<>(new ArrayList<>());
            }
        }

        List<HeadDto> list = headMapper.selectByPage(userId, title, museumId, activityId);
        return new PageInfo<>(list);
    }
}
